using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;

namespace One_Crappy_RPG
{
    public class Menu
    {
        public List<MenuItem> Items = new List<MenuItem>();
        public Vector2 Position = Vector2.Zero;
        int currentItem = 0;

        public MenuItem SelectedItem
        {
            get
            {
                if (Items.Count > 0)
                    return Items[currentItem];
                else
                    return null;
            }
        }

        public int SelectedIndex
        {
            get {return currentItem; }
            set {currentItem = value; }
        }

        public Menu(Vector2 position)
        {
            Position = position;
        }

        public void ClearItems()
        {
            foreach (MenuItem item in Items)
            {
                item.Name = string.Empty;
                item.UnhookEvents();
            }
        }

        public void AddItem(MenuItem item)
        {
            Items.Add(item);

            Items[Items.Count - 1].Position.X = this.Position.X;
            Items[Items.Count - 1].Position.Y = this.Position.Y + 18 * (Items.Count - 1) + 3;
        }

        public void SelectNext()
        {
            if (Items.Count > 0)
            {
                do
                {
                    currentItem = (currentItem + 1) % Items.Count;
                } while (!SelectedItem.Visible);
            }
        }



        public void SelectPrevious()
        {
            if (Items.Count > 0)
            {
                do
                {
                    currentItem--;

                    if (currentItem < 0)
                        currentItem = Items.Count - 1;

                } while (!SelectedItem.Visible);
            }
        }

        public void Draw(SpriteBatch spriteBatch)
        {

            for (int i = 0; i < Items.Count; i++)
            {
                MenuItem item = Items[i];

                if (item.Visible)
                {
                    Color color = Color.White;

                    if (!item.Enabled)
                        color = Color.DarkGray;
                    else if (item == SelectedItem)
                        color = Color.Yellow;                        


                    spriteBatch.DrawString(InputManager.Font,
                        item.Name,
                        item.Position,
                        color);
                }
            }
        }
    }
}
